#include<stdio.h>
#include "kjson.h"
#define EXPECT_EQ_BASE(res, expect, actual, format)\
    if(res == 0){\
        test_all++;\
        fprintf(stderr, "%s:%d: Expect: " format " Actual: " format "\n", __FILE__, __LINE__,expect, actual);\
    }else {\
        test_all++;\
        test_pass++;\
    }

// define your own specific test function
#define EXPECT_EQ_INT(actual, expect) \
   EXPECT_EQ_BASE(actual == expect, expect, actual, "%d") 
#define EXPECT_EQ_DOUBLE(actual, expect) \
    EXPECT_EQ_BASE(actual == expect, expect, actual, "%lf")
#define TEST_NUM(expect, str) \
    kjson_parse(val, str); \
    EXPECT_EQ_DOUBLE(kjson_get_num(val), expect)
#define TEST_ERROR(error, str) \
    EXPECT_EQ_INT(kjson_parse(val, str), error)
static int test_pass = 0;
static int test_all = 0;
KJsonVal val = NULL;
char ** endp;
void test_parse_num()
{
    TEST_NUM(0.0, "0");
    TEST_NUM(0.0, "-0");
    TEST_NUM(0.0, "-0.0");
    TEST_NUM(1.0, "1");
    TEST_NUM(-1.0, "-1");
    TEST_NUM(1.5, "1.5");
    TEST_NUM(-1.5, "-1.5");
    TEST_NUM(3.1416, "3.1416");
    TEST_NUM(1E10, "1E10");
    TEST_NUM(1e10, "1e10");
    TEST_NUM(1E+10, "1E+10");
    TEST_NUM(1E-10, "1E-10");
    TEST_NUM(-1E10, "-1E10");
    TEST_NUM(-1e10, "-1e10");
    TEST_NUM(-1E+10, "-1E+10");
    TEST_NUM(-1E-10, "-1E-10");
    TEST_NUM(1.234E+10, "1.234E+10");
    TEST_NUM(1.234E-10, "1.234E-10");
    TEST_NUM(0.0, "1e-10000"); /* must underflow */
    TEST_ERROR(KJSON_INVALID_SYNTAX, "+0");
    TEST_ERROR(KJSON_INVALID_SYNTAX, "+1");
    TEST_ERROR(KJSON_INVALID_SYNTAX, ".123"); /* at least one digit before '.' */
    TEST_ERROR(KJSON_INVALID_SYNTAX, "1.");   /* at least one digit after '.' */
    TEST_ERROR(KJSON_INVALID_SYNTAX, "INF");
    TEST_ERROR(KJSON_INVALID_SYNTAX, "inf");
    TEST_ERROR(KJSON_INVALID_SYNTAX, "NAN");
    TEST_ERROR(KJSON_INVALID_SYNTAX, "nan");
}


int main()
{
    val = (KJsonVal)malloc(sizeof(struct KJsonVal));
    kjson_parse(val, "true");
    EXPECT_EQ_INT(val->type, KJSON_TRUE);
    kjson_parse(val, "   false");
    EXPECT_EQ_INT(val->type, KJSON_FALSE);
    kjson_parse(val, "null");
    EXPECT_EQ_INT(val->type, KJSON_NULL);
    test_parse_num();     
    printf("%d/%d, %3.2f%% passed\n", test_pass, test_all, ((float)test_pass/test_all) * 100);           
}